#ifndef _MYSQL_CONNECTION_
#define _MYSQL_CONNECTION_

//c++
#include <iostream>
#include <string>
#include <list>
#include <memory>
#include <functional>

//mysql driver
#include <mysql_driver.h>
#include <mysql_connection.h>

//mysql execute
#include <cppconn/driver.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <exception>

//thread mutex 
#include <mutex>

using delFunc = std::function<void(sql::Connection*)>;

class ConnectionPool
{
public:
	//获取数据库连接池对象 static单例模式
	static ConnectionPool* getInstance();
	//得到一条连接
	auto getConnect()->std::shared_ptr<sql::Connection>;
	//归还一条连接
	auto disConnect(std::shared_ptr<sql::Connection> &ret)->void;
	~ConnectionPool();


private:
	ConnectionPool(std::string name, std::string pwd, std::string nurl, int maxSize);
	//初始化连接池
	auto initConnectPool(int initialSize)->void;
	//毁坏连接池
	auto destoryPool()->void;
	//destory one connection
	auto destoryOneConn()->void;
	//扩大数据库连接池
	auto expandPool(int size)->void;
	//缩小数据库连接池
	auto reducePool(int size)->void;
	//add conn
	auto addConn(int size)->void;
public:
	//get size
	auto getPoolSize()->int;

private:
	std::string username; //帐号
	std::string password; //密码
	std::string url;      //连接url
	int poolSize;         //pool size

	std::list<std::shared_ptr<sql::Connection>> conList;  //存放所有连接

	static ConnectionPool *pool;//连接池对象
	std::mutex lock; //线程锁
	//Driver *driver;//mysql driver
	sql::Driver  *driver;  //mysql driver
};

#endif